﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Text_Adventures
{
    public static class Combat
    {
        static Random rand = new Random();
        public static bool Battle(Hero h, Character c)
        {
            bool result = false;
            //Console.Clear();
            while (c.CHP > 0 && h.CHP > 0)
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Attack(h, c);
                if (!(c.CHP > 0 && h.CHP > 0))
                    break;
                Console.BackgroundColor = ConsoleColor.Red;
                Attack(c, h);
            }
            if (h.CHP > 0)
                result = true;

            return result;
        }



        private static void Attack(BaseCreature attacker, BaseCreature defender)
        {
            //Rola o dado da Accuracy do attacker
            int acc = rand.Next(0, 100);
            if (acc <= attacker.Acc)
            {
                //Rola o dado da evasion do defender
                int eva = rand.Next(0, 100);
                if (eva >= defender.Eva)
                {
                    int damage;
                    //Rola o dado do dano
                    if (attacker.ActiveWeapon != null)
                    {
                         damage = rand.Next(1, attacker.ActiveWeapon.Atk + 1) + (attacker.Str - 10) / 2;
                    }
                    else
                         damage = rand.Next(1, 3) + (attacker.Str - 10) / 2;

                    damage -= defender.Def;
                    defender.CHP -= damage;
                    Console.WriteLine(attacker.Name + " atacou " + defender.Name + "! Causou " + damage+"!\n");
                }
                else
                    Console.WriteLine(defender.Name + " desviou o ataque de " + attacker.Name + "!\n");

            }
            else
            Console.WriteLine(attacker.Name + " errou " + defender.Name + "!\n");

        }
    }
}
